﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace Wys.DataForm.CSharp
{
    public partial class SaveDatabaseLocal : Wys.Control.Vb.AutoAdjustFrom
    {
        public SaveDatabaseLocal()
        {
            InitializeComponent();
        }

        private void btnBackUpDatabase_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            //增加备份文件  数据库 图片 和 附件 单独 备份

            //目录格式为 WYSDATA2009 SQLITEDATA Images UpLoad    
            //拷贝文件
            String strCopyPath = System.IO.Path.Combine(this.GetCurrentDirectory(), "Backup");

            if (!System.IO.Directory.Exists(strCopyPath))
            {
                System.IO.Directory.CreateDirectory(strCopyPath);
            }
            else
            {
                System.IO.Directory.Delete(strCopyPath, true);
                System.IO.Directory.CreateDirectory(strCopyPath);
            }

            //复制的主文件夹
            strCopyPath = System.IO.Path.Combine(strCopyPath, "WYSDATA2009");
            if (!System.IO.Directory.Exists(strCopyPath))
            {
                System.IO.Directory.CreateDirectory(strCopyPath);
            }

            //数据库文件夹 
            String strData = System.IO.Path.Combine(strCopyPath, "SQLITEDATA");
            if (!System.IO.Directory.Exists(strData))
            {
                System.IO.Directory.CreateDirectory(strData);
            }
            //开始复制文件
            if (chkGroup3Database.Checked)
            { 
                String strPath = Wys.Db.Vb.CTable.GetDatabasePath();
                System.IO.FileInfo fi = new System.IO.FileInfo(strPath);
                String strPathParent = fi.Directory.FullName;
                String strTimeStr = String.Format("{0:00}Y{1:00}M{2:00}D{3:00}H{4:00}M{5:00}S", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
                String str1 = fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length) + fi.Extension;
                str1 = System.IO.Path.Combine(strData, str1);
                if (System.IO.File.Exists(str1))
                {
                    System.IO.File.Delete(str1);
                }          
                fi.CopyTo(str1, true);
            }
            //压缩的文件路径
            String strRar = "";
            String strTimeStr1 = String.Format("{0:00}Y{1:00}M{2:00}D{3:00}H{4:00}M{5:00}S", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
            strRar = "WYSBACKUP" + strTimeStr1 + ".RAR"; 
            String strConfig = this.GetAppSettingConfig("DataBaseBackUp");
            strConfig = System.IO.Path.Combine(strConfig, "DatabaseBackUp");
            if (strConfig.TrimEnd() == "")
            {
                ShowMessageBox("请设置备份路径");
                return;
            }

            if (!System.IO.Directory.Exists(strConfig))
            {
                System.IO.Directory.CreateDirectory(strConfig);
            }

            strRar = System.IO.Path.Combine(strConfig, strRar);

            //附件文件夹
            if (chkGroup3Att.Checked)
            {
                String strAtt = System.IO.Path.Combine(strCopyPath, "UpLoad");
                if (!System.IO.Directory.Exists(strAtt))
                {
                    System.IO.Directory.CreateDirectory(strAtt);
                }
                //开始复制文件                 
                String strAtt1 = System.IO.Path.Combine(this.GetCurrentDirectory(), "UpLoad");
                if (System.IO.Directory.Exists(strAtt1))
                {
                    foreach (String fin in System.IO.Directory.GetFiles(strAtt1))
                    {
                       System.IO.File.Copy(fin, System.IO.Path.Combine(strAtt, System.IO.Path.GetFileName(fin)), true);
                    }
                }
            }          
            //图片文件夹
            if (chkGroup3Image.Checked)
            {
                String strImage = System.IO.Path.Combine(strCopyPath, "Images");
                if (!System.IO.Directory.Exists(strImage))
                {
                    System.IO.Directory.CreateDirectory(strImage);
                }
                //开始复制文件                
                String strImage1 = System.IO.Path.Combine(this.GetCurrentDirectory(), "Images");
                if (System.IO.Directory.Exists(strImage1))
                {
                    foreach (String fin in System.IO.Directory.GetFiles(strImage1))
                    {
                       System.IO.File.Copy(fin, System.IO.Path.Combine(strImage, System.IO.Path.GetFileName(fin)), true);
                    }

                }
            }           
            Rar(strCopyPath + @"\", strRar);            
            this.Enabled = true;
            System.IO.Directory.Delete(strCopyPath, true);   
        }

        private void btnBackupSoftware_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            try
            {

                String strPath = System.IO.Path.Combine(this.GetCurrentDirectory(), "SoftWare");

                String strTimeStr = String.Format("{0:00}Y{1:00}M{2:00}D{3:00}H{4:00}M{5:00}S", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
                String str1 = strPath;

                String strParentFolder = System.IO.Path.Combine(System.IO.Path.Combine(this.GetAppSettingConfig("SoftWareBackUp"), "SoftWareBackUp"), strTimeStr);

                String strRar = System.IO.Path.Combine(System.IO.Path.Combine(this.GetAppSettingConfig("SoftWareBackUp"), "SoftWareBackUp"), strTimeStr + "SoftWare.RAR");

                if (!System.IO.Directory.Exists(strParentFolder))
                {
                    System.IO.Directory.CreateDirectory(strParentFolder);
                }               

                txtSoftware.Items.Add("开始压缩小软件文件夹......");
                txtSoftware.Items.Add("开始拷贝小软件文件夹:" + str1 + "......");
                
                if (UTIL.CUtil.CopyFolder(this.Handle,strPath, strParentFolder)== 0)
                {                    
                    txtSoftware.Items.Add("拷贝小软件文件夹:" + str1 + "成功");
                    txtSoftware.Items.Add("开始压缩小软件文件夹:" + strRar + "......");

                    RarFolder(strParentFolder, strRar);

                    txtSoftware.Items.Add("压缩小软件文件夹:" + strRar + "成功");
                    txtSoftware.Items.Add("开始删除备份小软件文件夹:" + strRar + "......");
                    UTIL.CUtil.DeleteFolder(this.Handle, strParentFolder);
                    
                    txtSoftware.Items.Add("删除备份小软件文件夹:" + strRar + "成功");
                }
                else
                {
                    txtSoftware.Items.Add("失败，已经退出");
                    //System.IO.Directory.Delete(strParentFolder, true);
                    UTIL.CUtil.DeleteFolder(this.Handle,strParentFolder);
                }
                this.Enabled = true;              

            }
            catch (Exception me)
            {
                //SaveException(me);//D-2009-10-11
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
            }
            this.Enabled = true;
        }

        private void btnBackupAttachment_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            this.Enabled = true;
        }

        #region 调用WINRAR压缩 A-2009-09-12
        /// <summary>
        /// 调用WINRAR压缩 
        /// </summary>
        /// <param name="strSource">要压缩的文件</param>
        /// <param name="strTarget">压缩后的文件</param>
        /// <returns></returns>
        /// 0 是失败
        /// 1 是成功
        public int Rar(String strSource, String strTarget)
        {
            int iRusult = 0;
            try
            {
                System.Diagnostics.Process Process1 = new System.Diagnostics.Process();
                Process1.StartInfo.FileName = "Winrar.exe";
                Process1.StartInfo.CreateNoWindow = true;
                Process1.StartInfo.Arguments = " a -r -ep1  \"" + strTarget + "\"  \"" + strSource + "\"";
                Process1.Start();
                Process1.WaitForExit();
                if (Process1.HasExited)
                {
                    int iExitCode = Process1.ExitCode;
                    if (iExitCode == 0)
                    {
                        //正常完成
                        iRusult = 1;
                    }
                    else
                    {
                        //有错
                        iRusult = 0;
                    }
                }
                Process1.Close();
                return iRusult;
            }
            catch (Exception me)
            {
                //SaveException(me);//D-2009-10-11
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
                iRusult = 0;
                return iRusult;
            }
        }
        #endregion

        #region 调用WINRAR压缩 A-2009-09-13
        /// <summary>
        /// 调用WINRAR压缩 
        /// </summary>
        /// <param name="strSource">要压缩的文件</param>
        /// <param name="strTarget">压缩后的文件</param>
        /// <returns></returns>
        /// 0 是失败
        /// 1 是成功
        public int RarFolder(String strSource, String strTarget)
        {
            int iRusult = 0;
            try
            {
                System.Diagnostics.Process Process1 = new System.Diagnostics.Process();
                Process1.StartInfo.FileName = "Winrar.exe";
                Process1.StartInfo.CreateNoWindow = true;
                Process1.StartInfo.Arguments = " a -r   \"" + strTarget + "\"   \"" + strSource+"\"";
                Process1.Start();
                Process1.WaitForExit();
                if (Process1.HasExited)
                {
                    int iExitCode = Process1.ExitCode;
                    if (iExitCode == 0)
                    {
                        //正常完成
                        iRusult = 1;
                    }
                    else
                    {
                        //有错
                        iRusult = 0;
                    }
                }
                Process1.Close();
                return iRusult;
            }
            catch (Exception me)
            {
                ////SaveException(me);//D-2009-10-11
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
                iRusult = 0;
                return iRusult;
            }
        }
        #endregion
        private void btnDatabaseOpen_Click(object sender, EventArgs e)
        {
            String strPath = this.GetAppSettingConfig("DataBaseBackUp");
            String strPath1 = System.IO.Path.Combine(this.GetAppSettingConfig("DataBaseBackUp"), "DatabaseBackUp");
            if (System.IO.Directory.Exists(strPath))
            {
                if (System.IO.Directory.Exists(strPath1))
                {
                    System.Diagnostics.Process.Start(strPath1);
                }
                else
                {
                    System.Diagnostics.Process.Start(strPath);
                }
            }
        }

        private void btnSoftWareOpen_Click(object sender, EventArgs e)
        {
            String strPath = this.GetAppSettingConfig("SoftWareBackUp");
            String strPath1 = System.IO.Path.Combine(this.GetAppSettingConfig("SoftWareBackUp"), "SoftWareBackUp");
            if (System.IO.Directory.Exists(strPath))
            {
                if (System.IO.Directory.Exists(strPath1))
                {
                    System.Diagnostics.Process.Start(strPath1);
                }
                else
                {
                    System.Diagnostics.Process.Start(strPath);
                }
            }
        }

        private void btnAttchmentOpen_Click(object sender, EventArgs e)
        {
            String strPath = this.GetAppSettingConfig("AttachmentBackUp");
            String strPath1 = System.IO.Path.Combine(this.GetAppSettingConfig("AttachmentBackUp"), "AttachmentBackUp");
            if (System.IO.Directory.Exists(strPath))
            {
                if (System.IO.Directory.Exists(strPath1))
                {
                    System.Diagnostics.Process.Start(strPath1);
                }
                else
                {
                    System.Diagnostics.Process.Start(strPath);
                }
            }
        }
    }
}
